##!/bin/bash
################################################################################
# Script to plot a CLEANPP data file
################################################################################
# Bmort --- bmort@astro.ox.ac.uk ---                     last update: 02 OCT 07
################################################################################
# COMMAND LINE INPUTS:
#
# 1. spect directory (location of the spect directory relating to the data)
# 2. plot title (main title of the plot)
# 3. plot id
################################################################################

############################################################################
#HARDCODED INPUTS
############################################################################
DATA_NAME="indata.dat"
#DATA_NAME="";
XMIN=1
XMAX=1
YMIN=1
YMAX=1
XTICS=0.1   # x tick increment
MXTICS=5    # number of sub tics between x tics
MYTICS=2    # number of sub tics between y tics
COLX=6      # x column data
COLY=4      # y column data
PLOT_NAME="data" # plot name root, used to form the plot name
###########################################################################
XLABEL="time (days)"
YLABEL="speed (c)"
YLABEL2=""
###########################################################################
echo ""
echo "################# PLOT DATA ##################################"

#ARG CHECK
if [ $# -ne 3 ]
    then                                                                        
    echo "\n";                                                                  
    echo "ERROR: missing form command line arguments. ($# out of 3 found)"                          
    echo "USAGE: $> sh PLOTdata.sh [DIR] [TITLE] [PLOT ID]"     
    echo ""
    echo "";                                                                  
    exit 1                                                                      
fi  

#COMMAND LINE ARGS
DIR=$1
TITLE=$2
ID=$3

#PROCESSING INPUTS
DATA_FILE=${DIR}/${DATA_NAME}
PLOT_NAME=${PLOT_NAME}"-$ID"
PLOT_FILE=${DIR}"/PLOT_${PLOT_NAME}.ps"
PLOT_SCRIPT=${DIR}"/${PLOT_NAME}.plot"

#CHECK TO SEE IF DATA EXISTS
if [ -f $DATA_FILE ]
then
    echo "" # FILE EXISTS
else
  echo "DATA FILE DOES NOT EXIST... exiting!\n"
  exit
fi

echo "=================================================="
echo "$0 [DATA DIR] [TITLE] [PLOT NAME]"
echo "=================================================="
echo "number of args found = $#"
echo "DIR         = $DIR"
echo "PLOT ID     = $ID"
echo "PLOT NAME   = $PLOT_NAME"
echo "PLOT SCRIPT = $PLOT_SCRIPT"
echo "PLOT FILE   = $PLOT_FILE"
echo "TITLE       = $TITLE"
echo "=================================================="
echo "** HARDCODED VALUES ** CHECK THESE **"
echo "XMIN       = ${XMIN}"
echo "XMAX       = ${XMAX}"
echo "YMIN       = ${YMIN}"
echo "YMAX       = ${YMAX}"
echo "XTICS      = ${XTICS}"
echo "MXTICS     = ${MXTICS}"
echo "MYTICS     = ${MYTICS}"
echo "COLX       = ${COLX}"
echo "COLY       = ${COLY}"
echo "=================================================="
echo ""


plot_file_template () { # 1(data file) 2(title) 3(ps plot file)
    echo "##################################################################"
    echo "# gnuplot script file to plot speed data                          "
    echo "#-----------------------------------------------------------------"
    echo "#                                                  bmort - 02OCT07"
    echo "##################################################################"
    echo ""
    echo "reset"
    echo "set terminal postscript landscape enhanced color dashed lw 1 \"Helvetica\" 11"
    echo "set output \"TMPplot.ps\""
    echo "unset key"
    echo "unset label"
    echo "set grid xtics ytics"
#    echo "set xtics ${XTICS}"
    echo "set mxtics ${MXTICS}"
    echo "set mytics ${MYTICS}"
    echo "set lmargin 14"
    echo "set rmargin 4"
    echo "set bmargin 5"
    echo "set tmargin 6"
    echo "set ylabel \"${YLABEL}\""
    echo "set xlabel \"${XLABEL}\""
    echo "set title \"$2\" font \"Helvetica,16\""
#    echo "set xrange [${XMIN}:${XMAX}]"
#    echo "set yrange [${YMIN}:${YMAX}]"
    echo "plot \"$1\" u 2:3 w p"
    echo "set terminal x11"
    echo "!mv TMPplot.ps $3"
    echo ""
}

make_plot_file () {
    echo "make plot file ..."
    echo "1 data file    = ${1}"
    echo "2 graph title  = ${2}"
    echo "3 plot file    = ${3}"
    echo "4 plot script  = ${4}"
    plot_file_template $1 "${2}" $3 > $4
}

make_data_plot() {
    make_plot_file $1 "${2}" $3 $4
    echo ""
    echo "==> Loading gnuplot script file <=="
    gnuplot $4
}


make_data_plot ${DATA_FILE} "${TITLE}" ${PLOT_FILE} ${PLOT_SCRIPT}
echo ""
echo "-- DONE"
echo "========================================================"
echo "gv ${PLOT_FILE}"
echo "emacs -nw ${PLOT_SCRIPT}"
echo "cp ${PLOT_FILE}"
echo "========================================================"
